<template>
  <div class="app-container">
    <Search @reset="reset" @search="handleSearch">
      <div>
        <label>邮箱</label>
        <el-input v-model="searchParams.email" placeholder="请输入" clearable @change="handleSearch"></el-input>
      </div>
      <div>
        <label>昵称</label>
        <el-input v-model="searchParams.nickname" placeholder="请输入" clearable @change="handleSearch"></el-input>
      </div>
    </Search>
    <vxe-grid v-bind="gridConfig" class="sticky-header">
      <template #toolbar_buttons>
        <el-button icon="PlusIcon" @click="operateRef.open({ type: 'add' })">新增</el-button>
      </template>
      <template #photo="{ row, rowIndex }">
        <el-image
          style="width: 40px; height: 40px"
          :src="row.avatar"
          :zoom-rate="1.2"
          :preview-src-list="previewSrcList"
          :initial-index="rowIndex"
          fit="cover"
        />
      </template>
      <template #operate="{ row }">
        <el-button link type="primary" @click="operateRef.open({ type: 'edit', user_id: row.user_id })">修改</el-button>
        <el-button link @click="operateRef.open({ type: 'detail', user_id: row.user_id })">详情</el-button>
        <el-popconfirm title="确定删除吗?" @confirm="handleDelete(row.user_id)">
          <template #reference>
            <el-button link type="danger">删除</el-button>
          </template>
        </el-popconfirm>
      </template>
      <template #pager>
        <vxe-pager
          v-model:current-page="searchParams.pageNum"
          v-model:page-size="searchParams.pageSize"
          :total="searchParams.total"
          @page-change="getList"
        >
        </vxe-pager>
      </template>
    </vxe-grid>
  </div>

  <operate-component ref="operateRef" @refresh-list="getList"></operate-component>
</template>

<script lang="ts" setup>
import Search from "@/components/Search/index.vue"
import { reactive, onMounted, ref } from "vue"
import { VxeGridProps } from "vxe-table"
import { ElMessage } from "element-plus"
import { getUserListByPage, deleteUser } from "@/api/userManage/index"
import operateComponent from "./operate.vue"

let previewSrcList = ref([])
const operateRef = ref()
const searchParams = reactive(setDefaultSearchParams())

const gridConfig = reactive<VxeGridProps>({
  id: "User", // 表格唯一标识
  customConfig: {
    storage: true
  },
  toolbarConfig: {
    custom: true,
    slots: {
      buttons: "toolbar_buttons"
    }
  },
  columns: [
    { type: "seq", width: 50 },
    { title: "用户id", field: "user_id" },
    { title: "邮箱", field: "email" },
    { title: "昵称", field: "nickname" },
    { title: "密码", field: "password" },
    {
      title: "头像",
      field: "avatar",
      slots: {
        default: "photo"
      }
    },
    { title: "日期", field: "createdAt" },
    { title: "操作", field: "operate", slots: { default: "operate" }, showOverflow: false, width: 180 }
  ],
  data: []
})

function handleDelete(user_id: number) {
  deleteUser({ user_id }).then(() => {
    ElMessage.success("删除成功")
    getList()
  })
}

function handleSearch() {
  searchParams.pageNum = 1
  getList()
}

function reset() {
  Object.assign(searchParams, setDefaultSearchParams())
  handleSearch()
}

function setDefaultSearchParams() {
  return {
    pageNum: 1,
    pageSize: 10,
    email: "",
    nickname: "",
    total: 0
  }
}

function getList() {
  getUserListByPage(searchParams).then((res: any) => {
    gridConfig.data = res.data.list
    searchParams.total = res.data.total
    previewSrcList = res.data.list.map((item: { avatar: any }) => item.avatar)
  })
}

onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped></style>
